package zetcode.sprites;

import java.awt.Color;
import java.awt.Font;
//import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class SpritesPanel extends JPanel implements ActionListener {

	private Timer timer;
	private Craft craft;
	private ArrayList<Alien> aliens;
	private boolean inGame;
	
	//private int PANEL_WIDTH;
	//private int PANEL_HEIGHT;
	
	private int[][] pos = {{380, 50}, {420, 100}, {500, 200}};
	
	public SpritesPanel(){
		addKeyListener(new TAdapter());
		setFocusable(true);
		setBackground(Color.BLACK);
		setDoubleBuffered(true);
		inGame = true;
		
		craft = new Craft();
		initAliens();
		
		timer = new Timer(5, this);
		timer.start();
	}
	
	public void addNotify(){
		super.addNotify();
		//PANEL_WIDTH = getWidth();
		//PANEL_HEIGHT = getHeight();
	}
	
	public void initAliens(){
		aliens = new ArrayList<Alien>();
		for(int i = 0; i < pos.length; i++){
			aliens.add(new Alien(pos[i][0], pos[i][1]));
		}
	}
	
	public void paint(Graphics g){
		super.paint(g);
		
		if(inGame){
			Graphics2D g2d = (Graphics2D) g;
			
			if(craft.isVisible()){g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);}
			
			ArrayList<Missile> ms = craft.getMissiles();
			
			for(int i = 0; i < ms.size(); i++){
				Missile m = ms.get(i);
				g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
			}
			
			for(int i = 0; i < aliens.size(); i++){
				Alien a = aliens.get(i);
				if(a.isVisible()){g2d.drawImage(a.getImage(), a.getX(), a.getY(), this);}
			}
			
			g2d.setColor(Color.WHITE);
			g2d.drawString("Aliens left: " + aliens.size(), 5, 15);
		}
		else{
			String msg = "Game Over!";
			Font small = new Font("Helvetica", Font.BOLD, 14);
			//FontMetrics fm = this.getFontMetrics(small);
			
			g.setColor(Color.WHITE);
			g.setFont(small);
			g.drawString(msg, 150, 80);
		}
		
		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(aliens.size() == 0){inGame = false;}
		
		ArrayList<Missile> ms = craft.getMissiles();
		for(int i = 0; i < ms.size(); i++){
			Missile m = ms.get(i);
			if(m.isVisible()){m.move();}
			else{ms.remove(i);}
		}
		
		for(int i = 0; i < aliens.size(); i++){
			Alien a = aliens.get(i);
			if(a.isVisible()){a.move();}
			else{aliens.remove(i);}
		}
		
		craft.move();
		checkCollisions();
		repaint();
	}
	
	public void checkCollisions(){
		Rectangle craftBounds = craft.getBounds();
		
		for(int i = 0; i < aliens.size(); i++){
			Alien a = aliens.get(i);
			Rectangle alienBounds = a.getBounds();
			
			if(craftBounds.intersects(alienBounds)){
				craft.setVisible(false);
				a.setVisible(false);
				inGame = false;
			}
		}
		
		ArrayList<Missile> ms = craft.getMissiles();
		
		for(int i = 0; i < ms.size(); i++){
			Missile m = ms.get(i);
			Rectangle missileBounds = m.getBounds();
			
			for(int j = 0; j < aliens.size(); j++){
				Alien a = aliens.get(j);
				Rectangle alienBounds = a.getBounds();
				
				if(missileBounds.intersects(alienBounds)){
					m.setVisible(false);
					a.setVisible(false);
				}
			}
		}
		
	}
	
	private class TAdapter extends KeyAdapter {
		public void keyReleased(KeyEvent e){craft.keyReleased(e);}
		public void keyPressed(KeyEvent e){craft.keyPressed(e);}
	}

}
